package com.gun.management.module.task.service.taskRewardLog;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gun.management.framework.mybatis.core.util.MyBatisUtils;
import com.gun.management.module.task.controller.admin.taskRewardRecord.vo.TaskRewardRecordRespVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.gun.management.module.task.controller.admin.taskRewardLog.vo.*;
import com.gun.management.module.task.dal.dataobject.taskRewardLog.TaskRewardLogDO;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.util.object.BeanUtils;

import com.gun.management.module.task.dal.mysql.taskRewardLog.TaskRewardLogMapper;

import static com.gun.management.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.gun.management.module.task.enums.ErrorCodeConstants.*;

/**
 * 任务奖励日志信息 Service 实现类
 *
 * @author 测试账号
 */
@Service
@Validated
public class TaskRewardLogServiceImpl implements TaskRewardLogService {

    @Resource
    private TaskRewardLogMapper rewardLogMapper;

    @Override
    public Long createRewardLog(TaskRewardLogSaveReqVO createReqVO) {
        // 插入
        TaskRewardLogDO rewardLog = BeanUtils.toBean(createReqVO, TaskRewardLogDO.class);
        rewardLogMapper.insert(rewardLog);
        // 返回
        return rewardLog.getId();
    }

    @Override
    public void updateRewardLog(TaskRewardLogSaveReqVO updateReqVO) {
        // 校验存在
        validateRewardLogExists(updateReqVO.getId());
        // 更新
        TaskRewardLogDO updateObj = BeanUtils.toBean(updateReqVO, TaskRewardLogDO.class);
        rewardLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteRewardLog(Long id) {
        // 校验存在
        validateRewardLogExists(id);
        // 删除
        rewardLogMapper.deleteById(id);
    }

    private void validateRewardLogExists(Long id) {
        if (rewardLogMapper.selectById(id) == null) {
            throw exception(REWARD_LOG_NOT_EXISTS);
        }
    }

    @Override
    public TaskRewardLogDO getRewardLog(Long id) {
        return rewardLogMapper.selectById(id);
    }

    @Override
    public PageResult<TaskRewardLogRespVO> getRewardLogPage(TaskRewardLogPageReqVO pageReqVO) {
        IPage<TaskRewardLogRespVO> pageResult = rewardLogMapper.selectTaskRewardRecordPageList(MyBatisUtils.buildPage(pageReqVO), pageReqVO.getTaskName(), pageReqVO.getCategoryId(),pageReqVO.getRewardTime(),pageReqVO.getUserName());
        return new PageResult<>(pageResult.getRecords(), pageResult.getTotal());
    }

}